/*
 * HublistGrabber.java
 *
 * Created on February 22, 2007, 7:05 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package hubsniffer.module;

import hubsniffer.Settings;
import hubsniffer.db.DataBaseManager;
import hubsniffer.hublist.Hublist;
import hubsniffer.hublist.db.HubEntryFetcher;
import hubsniffer.hublist.db.HubListFetcher;
import hubsniffer.hublist.db.HublistWriter;
import hubsniffer.sghdc.tasks.HublistSnifferTask;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import libjdc.hublist.ConfigHublistWriter;
import libjdc.hublist.HublistEntry;
import libjdc.hublist.XMLHublistWriter;

/**
 * This class is responsible to Manage Hublist's
 * @author dbotelho
 */
public class HublistGrabber {
    
    private static final String DEFAULT_FILENAME = "AllHublist";
    private Settings settings;
    private DataBaseManager db;
    private LinkedList<Hublist> hublists;
    
    /** 
     * Creates a new instance of HublistGrabber 
     * 
     * @param settings Defini��es da aplica��o
     * @throws java.sql.SQLException 
     * @throws java.io.UnsupportedEncodingException 
     * @throws java.lang.InterruptedException 
     * @throws java.io.IOException 
     * @param db Base de Dados j� inicializada
     */
    public HublistGrabber(Settings settings) throws SQLException, UnsupportedEncodingException, InterruptedException, IOException, ClassNotFoundException {
        this.settings = settings;
        db = new DataBaseManager(settings.getDB_ADDRESS(), settings.getDB_USERNAME(), settings.getDB_PASSWD());
            
        
        hublists = new HubListFetcher(this.db).getAll();
        
        Thread thread = new Thread(new HublistSnifferTask(this.db,hublists));
        thread.start();
        thread.join();
    }
    
    /**
     * M�todo que faz o Update na BD de todas as Hublists que foram testadas.
     */
    public void updateHublists(){
        System.out.println("Vai come�ar a escrever..");
        new HublistWriter(this.db).commit(hublists);
    }
    
    /**
     * Cria os Ficheiros XML.BZ2 e CONFIG.BZ2
     * 
     * @param forceUpdate Se estiver a true, primeiro � feito um update e so dp � q s�o gerados os ficheiros
     * @throws java.sql.SQLException 
     * @throws java.io.UnsupportedEncodingException 
     * @throws java.io.IOException 
     */
    public void generateHublistFiles(boolean forceUpdate) throws SQLException, UnsupportedEncodingException, IOException{
        
        if(forceUpdate)
            updateHublists();
        
        HublistEntry hublist = new HublistEntry("SGHDC - All Hubs "+new Date(),"http://sghdc.no-ip.org/");
        hublist.setHubEntries(new HubEntryFetcher(this.db).getAll());
        //Iterator<HubEntry> it = hublist.getHubEntries().iterator();
        XMLHublistWriter.writeXMLBZ2File(hublist,this.settings.getRESOURCE_PATH()+File.separatorChar+DEFAULT_FILENAME);
        new ConfigHublistWriter(hublist,this.settings.getRESOURCE_PATH()+File.separatorChar+"AllHublist.config");
        
    }
    
   public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException, FileNotFoundException {
        Settings settings = Settings.getInstance("/home/dbotelho/.hubsniffer");
        
        //DataBaseManager db = new DataBaseManager(settings.getDB_ADDRESS(), settings.getDB_USERNAME(), settings.getDB_PASSWD());
       /* LinkedList<Hublist> hublists = new LinkedList<Hublist>();
        hublists.add(new Hublist(1,"http://dchublist.com/hublist.xml.bz2",2,2));*/
        
        Thread thread = new Thread(new Hublist(1,"http://dchublist.com/hublist.xml.bz2",2,2));
        thread.start();
        thread.join();

    }
}
